package org.example.Server.service.homework.Impl;

import lombok.extern.slf4j.Slf4j;
import org.example.Server.mapper.homework.StuHmkMapper;
import org.example.Server.mapper.homework.TeacherHomeworkMapper;
import org.example.Server.model.homework.*;
import org.example.Server.service.homework.StuHmkService;
import org.example.Server.utils.ResultAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class StuHmkServiceImpl implements StuHmkService {
    @Autowired
    StuHmkMapper stuHmkMapper;
    @Autowired
    TeacherHomeworkMapper teacherHomeworkMapper;


    /**
     * 获取学生作业列表
     *
     * @param semesterId
     * @return
     */
    public ResultAPI getHmkStuList(Integer semesterId, Integer userId) {
        List<HmkStuListVO> hmkStuListVOS = stuHmkMapper.getHmkStuList(semesterId, userId);
        return ResultAPI.success(hmkStuListVOS);
    }

    /**
     * 学生提交作业
     *
     * @param hmkStu
     * @return
     */
    @Override
    public ResultAPI submitStuHmk(HmkStuDTO hmkStu) {
        stuHmkMapper.submitStuHmk(hmkStu);
        return ResultAPI.success(hmkStu);
    }

    /**
     * 互评显示
     *
     * @param hmkId
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public ResultAPI geteachList(Integer hmkId, Integer userId) {
        // 判断是否已经进行了互评分组
        // 已经分组：获取互评作业
        // a.根据课程设置返回互评数量
        Integer eachCnt = stuHmkMapper.getEachCnt(hmkId);
        List<HmkStuEach> currentList = stuHmkMapper.getStuEachList(hmkId, userId);
        if (!currentList.isEmpty()) {
            int nowCnt = currentList.size();
            // 分够了教师规定数量

            // 没分够教师规定数量
            if (nowCnt < eachCnt) {
                int cnt = eachCnt - nowCnt;
                // 继续分（不能把自己还有已知同学分进去了
                List<HmkStuEach> moreGroup = stuHmkMapper.getEachMoreGroup(currentList, hmkId, userId, cnt);
                if (!moreGroup.isEmpty()) {
                    stuHmkMapper.insertEachGroup(moreGroup, userId, hmkId);
                    // c. 获取作业具体列表
                    List<HmkStuEach> res = stuHmkMapper.getStuEachList(hmkId, userId);
                    return ResultAPI.success(res);
                }
            }
            return ResultAPI.success(currentList);
        } else {
            // 没分组：
            // 1.分组
            // b. 获取互评作业id列表
            List<HmkStuEach> hmkStuEaches = stuHmkMapper.getEachGroup(hmkId, userId, eachCnt);
            // 2.加入表
            if (!hmkStuEaches.isEmpty()) {
                stuHmkMapper.insertEachGroup(hmkStuEaches, userId, hmkId);
                // c. 获取作业具体列表
                List<HmkStuEach> res = stuHmkMapper.getStuEachList(hmkId, userId);
                return ResultAPI.success(res);
            } else {
                return ResultAPI.success(new ArrayList<HmkStuEach>());
            }
        }
    }

    /**
     * 学生修改作业
     *
     * @param hmkStu
     * @return
     */
    @Override
    public ResultAPI updateStuHmk(HmkStuDTO hmkStu) {
        stuHmkMapper.updateStuHmk(hmkStu);
        return ResultAPI.success();
    }

    /**
     * 学生获取给别人的作业详情
     *
     * @param stuHmkId
     * @param userId
     * @return
     */
    @Override
    public ResultAPI getEachDetail(Integer stuHmkId, Integer userId) {
        HmkStuEachVO hmkStuEachVO = stuHmkMapper.getEachDetail(stuHmkId, userId);
        return ResultAPI.success(hmkStuEachVO);
    }

    /**
     * 获取学生提交的作业
     *
     * @param stu_hmwk_id
     * @return
     */
    @Override
    public ResultAPI showSubmit(Integer stu_hmwk_id) {
        HmkStuVO hmkStuVO = stuHmkMapper.showSubmitHmk(stu_hmwk_id);
        Integer stage = stuHmkMapper.getHmkStage(stu_hmwk_id);
        hmkStuVO.setStage(stage);
        return ResultAPI.success(hmkStuVO);
    }

    /**
     * 获取作业概览
     *
     * @param userId
     * @param hmkId
     * @return
     */
    @Override
    public ResultAPI getStuHmkReview(Integer userId, Integer hmkId) {
        HmkStuReview hmkStuReview = stuHmkMapper.getStuHmkReview(userId, hmkId);
        Integer stuHmwkId = hmkStuReview.getStu_hmwk_id();
        //获取教师、助教、互评分数以及占比
        //获取该课堂分值占比以及是否选择学生互评
        HmkScoreDetail hmkScoreDetail = stuHmkMapper.getHmkScoreDetail(stuHmwkId);
        List<HmkEachGrade> hmkEachGrade = teacherHomeworkMapper.getStuEachScore(stuHmwkId);
        for (HmkEachGrade item : hmkEachGrade) {
            Double score = item.getEach_score();
            if (item.getIdentity() == 0) {
                hmkScoreDetail.setStuScore(score);
            } else if (item.getIdentity() == 1) {
                hmkScoreDetail.setTechScore(score);
            } else if (item.getIdentity() == 2) {
                hmkScoreDetail.setAdminScore(score);
            }
        }
        hmkStuReview.setHmk_score_detail(hmkScoreDetail);
        return ResultAPI.success(hmkStuReview);
    }

}
